package com.lp.RSTX.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import java.io.*;
import java.util.Enumeration;

@Slf4j
public  class CompressFileUtils {

    private CompressFileUtils(){}

    static String[] dirs;

    /**
     * 解压zip文件
     *
     * @param sourceFile ,待解压的zip文件; path,解压后的存放路径
     **/
    public static void zipFile(String path, String sourceFile) {

        ZipFile zfile = null;// 连接待解压文件
        log.info("连接待解压文件");
        try {
            zfile = new ZipFile(sourceFile, "GBK");
            log.info("￥￥￥￥");
            Enumeration<Object> zList = zfile.getEntries();
            log.info("得到zip包里的所有元素");// 得到zip包里的所有元素
            log.info("7.####");
            ZipEntry ze = null;

            byte[] buf = new byte[1024];
            log.info("...");
            while (zList.hasMoreElements()) {
                ze = (ZipEntry) zList.nextElement();
                log.info("8.####" + ze.toString());

                log.info("9.####" + path);
                if (ze.isDirectory()) {
                    continue;
                }
                OutputStream outputStream = null;
                InputStream inputStream = null;
                // 以ZipEntry为参数得到一个InputStream，并写到OutputStream中

                outputStream = new BufferedOutputStream(new FileOutputStream(
                        getRealFileName(path, ze.getName())));
                inputStream = new BufferedInputStream(zfile.getInputStream(ze));
                log.info("10.####" + outputStream.toString());
                int readLen = 0;
                while ((readLen = inputStream.read(buf, 0, 1024)) != -1) {
                    outputStream.write(buf, 0, readLen);
                }
                log.info("11.####");
                inputStream.close();
                outputStream.close();
                zfile.close();
            }

        } catch (IOException e) {
            log.info(e.toString());
            e.printStackTrace();
        }

    }


    /**
     * 给定根目录，返回一个相对路径所对应的实际文件名.
     *
     * @param zippath     指定根目录
     * @param absFileName 相对路径名，来自于ZipEntry中的name 2021-02-25//视频名字
     * /* @return java.io.File 实际的文件
     */

    private static File getRealFileName(String zippath, String absFileName) {
        dirs = absFileName.split("\\\\", absFileName.length());
        if (dirs.length == 1) {
            dirs = absFileName.split("/", absFileName.length());
        }
        log.info(dirs[0] + dirs[1]);
        File ret = new File(zippath);// 创建文件对象
        log.info(ret.toString());
        if (dirs.length > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                ret = new File(ret, dirs[i]);
                log.info("ret:" + ret.toString());
            }
        }
        if (!ret.exists()) {// 检测文件是否存在
            ret.mkdirs();// 创建此抽象路径名指定的目录
            log.info("创建此抽象路径名指定的目录");
        }
        ret = new File(ret, dirs[dirs.length - 1]);// 根据 ret 抽象路径名和 child
        // 路径名字符串创建一个新 File 实例
        log.info("路径名字符串创建一个新 File 实例");
        return ret;
    }

}